package com.foreveross.fetl.application.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.foreveross.fetl.application.ISinglePlaneInfoApplication;
import com.foreveross.fetl.core.domain.single.PlaneInfoJsonEntity;
import com.foreveross.fetl.core.domain.single.SinglePlaneInfoEntity;
import com.foreveross.fetl.util.JsonUtil;
import com.foreveross.fetl.vo.single.AgentVO;
import com.foreveross.fetl.vo.single.CabinVO;
import com.foreveross.fetl.vo.single.PlaneInfoJsonVO;
import com.foreveross.fetl.vo.single.SinglePlaneInfoVO;
import com.foreveross.fetl.vo.single.StopOverVO;
import com.foreveross.fetl.vo.single.TransitVO;
//@Transactional
@Service("singlePlaneInfoApplication")
public class SinglePlaneInfoApplicationImpl implements
		ISinglePlaneInfoApplication {

	@Override
	public void create(SinglePlaneInfoVO vo) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(SinglePlaneInfoVO vo) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public List<SinglePlaneInfoVO> findByCondition(String hbaseKey,
			String fromCity, String toCity, String flightBatchDate,
			String flightNo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SinglePlaneInfoVO> findPlaneInfoProperty(String hbaseKey,String carrierKey,
			String fromCity, String toCity, String flightBatchDate,
			String flightNo,String grabDate) {
		List<SinglePlaneInfoVO> listVo= new ArrayList<SinglePlaneInfoVO>();
		SinglePlaneInfoEntity entity =new SinglePlaneInfoEntity();
		List<SinglePlaneInfoEntity> listEntity = entity.findPlaneInfoProperty(hbaseKey,carrierKey, fromCity, toCity, flightBatchDate, flightNo,grabDate);
		if(null!=listEntity && listEntity.size()>0){
			for(SinglePlaneInfoEntity en: listEntity){
				SinglePlaneInfoVO vo =this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
		}
		return listVo;
	}

	@Override
	public List<SinglePlaneInfoVO> findPlanInfoAndFamily(String hbaseKey,
			String fromCity, String toCity, String flightBatchDate,
			String flightNo) {
		List<SinglePlaneInfoVO> listVo= new ArrayList<SinglePlaneInfoVO>();
		SinglePlaneInfoEntity entity =new SinglePlaneInfoEntity();
		List<SinglePlaneInfoEntity> listEntity = entity.findPlanInfoAndFamily(hbaseKey, fromCity, toCity, flightBatchDate, flightNo);
		if(null!=listEntity && listEntity.size()>0){
			for(SinglePlaneInfoEntity en: listEntity){
				SinglePlaneInfoVO vo =this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
		}
		return listVo;
	}
	
	public SinglePlaneInfoVO planeInfoEntityToVo(SinglePlaneInfoEntity entity){
		SinglePlaneInfoVO vo =new SinglePlaneInfoVO();
		vo.setActuallyFlightNo(entity.getActuallyFlightNo());
		vo.setAgentName(entity.getAgentName());
		vo.setAreaCode(entity.getAreaCode());
		vo.setGrabDate(entity.getGrabDate());
		vo.setAreaName(entity.getAgentName());
		vo.setAttachHbaseKey(entity.getAttachHbaseKey());
		vo.setCarrierFullName(entity.getCarrierFullName());
		vo.setCarrierKey(entity.getCarrierKey());
		vo.setCarrierName(entity.getCarrierName());
		vo.setCreateTime(entity.getCreateTime());
		vo.setEndTime(entity.getEndTime());
		vo.setFlightDate(entity.getFlightDate());
		vo.setFlightNo(entity.getFlightNo());
		vo.setFlightType(entity.getFlightType());
		vo.setFromCity(entity.getFromCity());
		vo.setFromCityName(entity.getFromCityName());
		vo.setGrabChannelId(entity.getGrabChannelId());
		vo.setGrabChannelName(entity.getGrabChannelName());
		vo.setId(entity.getId());
		vo.setSourceRowkey(entity.getSourceRowkey());
		vo.setStartTime(entity.getStartTime());
		vo.setToCity(entity.getToCity());
		vo.setToCityName(entity.getToCityName());
		vo.setTotalHighestPrice(entity.getTotalHighestPrice());
		vo.setTotalLowestPrice(entity.getTotalLowestPrice());
		if(null!=entity.getPlaneInfoJsonEntity()){
			vo.setPlaneInfoJsonVO(this.planeInfoJsonEntityToVO(entity.getPlaneInfoJsonEntity()));
			if(null!=vo.getPlaneInfoJsonVO().getAgentsJson()){
			  Set<AgentVO> setAgents =new HashSet<AgentVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getAgentsJson(), AgentVO.class));
			  vo.setAgents(setAgents);
			}
			if(null!=vo.getPlaneInfoJsonVO().getCabinsJson()){
				Set<CabinVO> setCabins =new HashSet<CabinVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getCabinsJson(), CabinVO.class));
				vo.setCabins(setCabins);
			}
			if(null!=vo.getPlaneInfoJsonVO().getTransitsJson()){
				Set<TransitVO> setTransit =new HashSet<TransitVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getTransitsJson(), TransitVO.class));
				vo.setTransits(setTransit);
			}
			if(null!=vo.getPlaneInfoJsonVO().getStopOversJson()){
				Set<StopOverVO> setStopOver =new HashSet<StopOverVO>(JsonUtil.getDTOList(vo.getPlaneInfoJsonVO().getStopOversJson(), StopOverVO.class));
				vo.setStopOvers(setStopOver);
			}
		}
		
		return vo;
	}
	
	public PlaneInfoJsonVO planeInfoJsonEntityToVO(PlaneInfoJsonEntity entity){
		PlaneInfoJsonVO vo =new PlaneInfoJsonVO();
		vo.setId(entity.getId());
		vo.setPlaneInfoId(entity.getId());
		vo.setAgentsJson(entity.getAgentsJson());
		vo.setCabinsJson(entity.getCabinsJson());
		vo.setStopOversJson(entity.getStopOversJson());
		vo.setTransitsJson(entity.getTransitsJson());
		return vo;
	}

	@Override
	public List<SinglePlaneInfoVO> findByGrabDate(String hbaseKey,
			String fromCity, String toCity, String grabDate) {
		List<SinglePlaneInfoVO> listVo= new ArrayList<SinglePlaneInfoVO>();
		SinglePlaneInfoEntity entity =new SinglePlaneInfoEntity();
		List<SinglePlaneInfoEntity> listEntity = entity.findByGrabDate(hbaseKey, fromCity, toCity, grabDate);
		if(null!=listEntity && listEntity.size()>0){
			for(SinglePlaneInfoEntity en: listEntity){
				SinglePlaneInfoVO vo =this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
		}
		return listVo;
	}

}
